package management;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;

import model.Candidato;

public class FileHelper {

	public static String readFile(String path, Charset encoding) 
			throws IOException 
			{
		byte[] encoded = Files.readAllBytes(Paths.get(path));
		return encoding.decode(ByteBuffer.wrap(encoded)).toString();
			}
	
	public static void writeFile(String path, Integer id, HashMap<String, Candidato> candidatesMap){
		File file = new File (path);
		PrintWriter printWriter;
		try {
			printWriter = new PrintWriter (file);
			printWriter.println ("<manager>");
			printWriter.println("<currentId>" + id + "</currentId>");
			printWriter.println("<candidatos>");
			for (Candidato candidate : candidatesMap.values()) {
				printWriter.println ("<candidato>");
				printWriter.println ("<id>" + candidate.getId() + "</id>");
				printWriter.println ("<nombre>" + candidate.getName() + "</nombre>");
				printWriter.println ("<cantVotos>" + candidate.getCantVotos() + "</cantVotos>");
				printWriter.println ("</candidato>");
			}
			printWriter.println ("</candidatos>");
			printWriter.println ("</manager>");
			printWriter.close ();       
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}
